package org.nuaa.client;

import jdk.nashorn.internal.scripts.JO;
import org.nuaa.client.Protocol;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;


import javax.swing.*;

/**
 * 基于TCP协议的Socket通信，实现文件传输-客户端
 */
public class Client {

    static JTextArea jta;
    private Socket socket;
    private static BufferedOutputStream bufferedOutputStream;

    public Client(JTextArea jta, Socket socket) {
        this.jta = jta;
        this.socket = socket;
    }

    public static boolean CheckSum(byte[] message, int len) {
        byte check = 0;
        for (int i = 0; i < len - 1; i++) {
            check = (byte) ((check + message[i]) % 256);
        }
        if (check == message[len - 1]) {
            return true;
        }
        return false;
    }

    public static byte[] getData(byte[] message, int len) {
        byte[] data = new byte[len];
        System.arraycopy(message, 14, data, 0, len);
        return data;
    }

    public void Download(int choice, double times, Integer accept) {
        try {
            OutputStream os = null;
            DataOutputStream dos = null;
            PrintWriter pw = null;


            // 先发送信号，是下载文件还是上传文件
            times = 1;
            dos = new DataOutputStream(socket.getOutputStream());
            dos.writeInt(accept.intValue());
            dos.flush();
            //dos.writeInt(1);

            InputStream is = socket.getInputStream();//有问题


            //以下都和下载有关
            BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
            dos = new DataOutputStream(socket.getOutputStream());

            System.out.println("准备放文件");
            String filePath = JOptionPane.showInputDialog("何处放文件？");

            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
            byte[] buf = new byte[271];
            int len = 0;
            int num = 0;
            times = 1e10;
            System.out.println(times);


            while (num < times && (len = bufferedInputStream.read(buf)) != -1) {

                if (CheckSum(buf, len)) {

                    byte[] data = getData(buf, len - 15);

                    bufferedOutputStream.write(data, 0, data.length);//写入文件

                    //jta.append("\n已完成第" + (num + 1) + "片的下载");

                    bufferedOutputStream.flush();
                    dos.writeInt(0);// 没出错
                    num++;
                } else {
                    jta.append("\n第" + (num + 1) + "片下载失败，进行重传");
                    dos.writeInt(1);
                }
            }

            System.out.println(60);
            bufferedOutputStream.flush();
            socket.shutdownInput();
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.println("文件已保存至客户端" + filePath);
            jta.append("\n文件已下载至客户端" + filePath
                    + "\n--------------------------------------------------\n");
            pw.flush();
            socket.shutdownOutput();

            if (pw != null) {
                pw.close();
            }
            if (os != null) {
                os.close();
            }
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
